package zyc.vpaddremove;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.*;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.Random;

public class MyActivity extends Activity {
    private ViewPager pager = null;
    private MainPagerAdapter pagerAdapter = null;
    private Random random;

    //-----------------------------------------------------------------------------
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        pagerAdapter = new MainPagerAdapter();
        pager = (ViewPager) findViewById(R.id.view_pager);
        pager.setAdapter(pagerAdapter);

        random = new Random();
        random.setSeed(0xffffffff);

        for (int i = 0; i < 11; i++) {
            pagerAdapter.addView(newView(String.valueOf(i)), i);
        }
    }

    public View newView(String text) {
        FrameLayout fl = new FrameLayout(this);
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(1280, 800);
        lp.gravity = Gravity.CENTER;
        fl.setLayoutParams(lp);

        TextView tv = new TextView(this);
        FrameLayout.LayoutParams tvLp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tvLp.gravity = Gravity.CENTER;
        tv.setLayoutParams(tvLp);
        tv.setTextSize(40);
        tv.setText(text);
        fl.addView(tv);

        fl.setBackgroundColor((int) random.nextLong());
        return fl;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.my_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int curPos = pager.getCurrentItem();
        switch (item.getItemId()) {
            case R.id.remove_left_sibling:
                pagerAdapter.removeView(pager, curPos - 1);
                break;
            case R.id.remove_right_sibling:
                pagerAdapter.removeView(pager, curPos + 1);
                break;
            case R.id.supplement_removed_page:
                pagerAdapter.addView(newView(String.valueOf(2)), 2);
                break;
        }
        pager.setCurrentItem(curPos);
        return true;
    }

    //-----------------------------------------------------------------------------
    // Here's what the app should do to add a view to the ViewPager.
    public void addView(View newPage) {
        int pageIndex = pagerAdapter.addView(newPage);
        // You might want to make "newPage" the currently displayed page:
        pager.setCurrentItem(pageIndex, true);
    }

    //-----------------------------------------------------------------------------
    // Here's what the app should do to remove a view from the ViewPager.
    public void removeView(View defunctPage) {
        int pageIndex = pagerAdapter.removeView(pager, defunctPage);
        // You might want to choose what page to display, if the current page was "defunctPage".
        if (pageIndex == pagerAdapter.getCount())
            pageIndex--;
        pager.setCurrentItem(pageIndex);
    }

    //-----------------------------------------------------------------------------
    // Here's what the app should do to get the currently displayed page.
    public View getCurrentPage() {
        return pagerAdapter.getView(pager.getCurrentItem());
    }

    //-----------------------------------------------------------------------------
    // Here's what the app should do to set the currently displayed page.  "pageToShow" must
    // currently be in the adapter, or this will crash.
    public void setCurrentPage(View pageToShow) {
        pager.setCurrentItem(pagerAdapter.getItemPosition(pageToShow), true);
    }
}
